﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Net;
using UMC.Web;
using UMC.Wiki.Entities;
using System.Collections;

namespace UMC.Wiki.Activities
{



    class SubjectParseActivity : WebActivity
    {



        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var Url = this.AsyncDialog("Url", g =>
            {
                this.Prompt("请输入URL");
                return this.DialogValue("none");
            });

            var url = this.AsyncDialog("Key", g =>
            {
                return this.DialogValue("none");
            });

            var ussr = this.Context.Token.Identity();

            var subId = UMC.Data.Utility.NewId();// Utility.UInt64Parse(Utility.TimeSpan(), Utility.IntParse(new Guid(UMC.Data.Utility.MD5(ussr.Id.Value, url))));


            if (url.StartsWith("none") == false)
            {

                var content = new Uri(url).WebRequest().Get(out var _); ;
                content = content.Replace((char)160, ' ');
                var cont = Data.JSON.Deserialize<Hashtable>(content);

                if (cont != null)
                {
                    if (cont.ContainsKey("markdown"))
                    {

                        var markdown = cont["markdown"] as string;

                        if (String.IsNullOrEmpty(markdown) == false)
                        {
                            var cells = Data.Markdown.Transform(markdown);
                            var dlist = new ArrayList();
                            foreach (var d in cells)
                            {
                                dlist.Add(new WebMeta().Put("type", d.Type).Put("value", d.Data).Put("format", d.Formats).Put("style", d.Style).GetDictionary());

                            }
                            var sub = new Subject()
                            {

                                Title = (cont["title"] as string ?? "").Trim(),
                                IsComment = false,
                                IsPicture = false,
                                Id = subId,
                                Url = Url,
                                LastDate = DateTime.Now,
                                Poster = ussr.Alias,
                                user_id = ussr.Id,
                                Status = -1
                            };
                            sub.Code = UMC.Data.Utility.Parse36Encode(Utility.IntParse(sub.Id.Value)[1]);
                            SubjectSaveActivity.Dashboard(ussr, sub, this.Context.AppKey ?? Guid.Empty);
                            DataFactory.Instance().Put(sub);


                            DataFactory.Instance().Put(new SubjectContent
                            {
                                DataJSON = Data.JSON.Serialize(dlist),
                                ContentType = "markdown",
                                Content = markdown,
                                Id = subId
                            });
                            this.Context.Send("Markdown", new WebMeta().Put("Id", sub.Id), true);
                        }
                    }
                    else
                    {
                        var data = cont["content"] as Array;

                        if (data != null)
                        {
                            var sub = new Subject()
                            {
                                Title = (cont["title"] as string ?? "").Trim(),
                                IsPicture = false,
                                Id = subId,
                                Url = Url,
                                LastDate = DateTime.Now,
                                IsComment = false,
                                Poster = ussr.Alias,
                                user_id = ussr.Id,
                                Status = -1
                            };
                            SubjectSaveActivity.Dashboard(ussr, sub, this.Context.AppKey ?? Guid.Empty);
                            sub.Code = UMC.Data.Utility.Parse36Encode(Utility.IntParse(sub.Id.Value)[0]);


                            DataFactory.Instance().Put(sub);

                            DataFactory.Instance().Put(new SubjectContent
                            {
                                DataJSON = Data.JSON.Serialize(data),
                                ContentType = "text/html",
                                Id = subId
                            });

                        }
                    }


                }
            }
            this.Context.Send(new UISectionBuilder(request.Model, "Content", new UMC.Web.WebMeta().Put("Id", subId).Put("Next", "Subject"))
                .CloseEvent("Subject.Save")
                    .Builder().Put("IsEditer", true), true);

        }
    }
}